package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.*;
import org.footballmanager.pandafootball.system.form.impl.FootRewardDetailSearchForm;
import org.footballmanager.pandafootball.system.service.FootRewardDetailService;
import org.footballmanager.pandafootball.system.service.FootRewardService;
import org.footballmanager.pandafootball.system.service.FootTaxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@SessionAttributes("footRewardDetailSearchForm")
@RequestMapping("/hy/footRewardDetail")
public class HyRewardDetailController extends BaseController {
    /* 实体名称 */
    private static final String ENTITYNAME = "footRewardDetail";
    private static final String ADDPAGE = String.format(Constants.ADDPAGE, Constants.HY, ENTITYNAME);
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);

    @Autowired
    private FootRewardDetailService footRewardDetailService;
    @Autowired
    private FootRewardService footRewardService;
    @Autowired
    private FootTaxService footTaxService;

    /**
     * 初始化
     *
     * @param map                 参数
     * @param footRewardDetailSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootRewardDetailSearchForm footRewardDetailSearchForm) {
        FootRewardDetailSearchForm newFootRewardDetailSearchForm = new FootRewardDetailSearchForm();
        if (footRewardDetailSearchForm != null) {
            newFootRewardDetailSearchForm = footRewardDetailSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootRewardDetailSearchForm);
    }

    /**
     * 抽奖记录详情列表
     *
     * @param map                 参数
     * @param footRewardDetailSearchForm 条件
     * @return 页面
     */
    @RequestMapping(value = "/search")
    public String search(Model map, HttpServletRequest request,
                         @ModelAttribute FootRewardDetailSearchForm footRewardDetailSearchForm) {
        Long userId = (Long) request.getSession().getAttribute(Constants.USERID);
        footRewardDetailSearchForm.setUserId(userId);
        footRewardDetailService.search(ENTITYNAME, map, footRewardDetailSearchForm);
        return LISTPAGE;
    }

    /**
     * 打开抽奖页面
     *
     * @param map   参数
     * @param request 请求
     * @return 页面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model map, HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute(Constants.USERID);
        FootReward reward = footRewardService.findByUserId(userId);
        int times = footTaxService.getFreeTimes();
        Double needMoney = reward.getNeedPay();
        map.addAttribute("needMoney", needMoney);
        if(times > reward.getGetTimesFree()){
            //剩余免费抽奖次数
            map.addAttribute("times", times - reward.getGetTimesFree());
        }else{
            map.addAttribute(Constants.MSG, "没有抽奖次数可以使用了");
        }
        return ADDPAGE;
    }

    /**
     * 免费抽奖
     *
     * @param map   参数
     * @param request 请求
     * @return 页面
     */
    @RequestMapping(value = "/rewardFree", method = RequestMethod.POST)
    @ResponseBody
    public JsonReturn rewardFree(Model map, HttpServletRequest request) {
        FootPerson p;
        Long userId = (Long) request.getSession().getAttribute(Constants.USERID);
        FootReward reward = footRewardService.findByUserId(userId);
        int times = footTaxService.getFreeTimes();
        if(times > reward.getGetTimesFree()){
            p = footRewardDetailService.rewardFree(userId, request);
        }else{
            map.addAttribute(Constants.MSG, "没有抽奖次数可以使用了");
            return new JsonReturn(Constants.ERROR);
        }
        JsonReturn jr = new JsonReturn(Constants.OK);
        jr.setObj(p);
        return jr;
    }

    /**
     * 花费抽奖
     *
     * @param map   参数
     * @param request 请求
     * @return 页面
     */
    @RequestMapping(value = "/reward", method = RequestMethod.POST)
    @ResponseBody
    public JsonReturn reward(Model map, HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute(Constants.USERID);
        FootPerson p = footRewardDetailService.reward(userId, request);
        if(p == null){
            return new JsonReturn(Constants.ERROR);
        }
        JsonReturn jr = new JsonReturn(Constants.OK);
        jr.setObj(p);
        return jr;
    }
}
